home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v8n14.arc / NOVIRUS.C < prev    next >
Text File  |  1989-07-06  |  3KB  |  120 lines

  1.  
  2. #include <stdio.h>
  3. #include <malloc.h>
  4.  
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define BLOCKS 20
  8.  
  9. struct avirus {
  10.     char code[8];
  11.     int bytes;
  12.     unsigned long lowcrc;
  13.     int crcs[BLOCKS];
  14. } antivir = { "CAPRIO\xDF\0" };
  15.  
  16. main(int argc, char *argv[])
  17. {
  18.     printf("This shows how a program may perform a self-check at startup\n");
  19.     printf("to protect itself from being infected by a virus.\n\n");
  20.  
  21.     switch (check(argv)) {
  22.         case 0 :
  23.             printf("Executable is unaltered\n");
  24.             break;
  25.         case 1 :
  26.             printf("Could not find executable\n");
  27.             break;
  28.         case 2 :
  29.             printf("Executable has not been PATCHed\n");
  30.             break;
  31.         case 3 :
  32.             printf("Executable has been altered, possibly by a virus\n");
  33.             exit(1);
  34.         }        
  35.  
  36.     /*--------------------------*/
  37.     /* Insert body of code here */
  38.     /*--------------------------*/
  39. }
  40.  
  41. check(char *argv[])    /* Function called to perform the CRC check */
  42. {
  43.     int crc;
  44.     char *buff, *buffp;        /* Pointers to data buffer */
  45.     int j1, j2 = 0;
  46.     int change = FALSE;        /* TRUE if file altered */
  47.     int rsize;            /* Amount of data read into buffer */
  48.     long fl;            /* File length */
  49.     FILE *fn;
  50.     unsigned long bytecount = 0;    /* Position in file */
  51.     unsigned long lowcrc, highcrc;    /* Position of areas patched */
  52.  
  53.     /* Open executable file for reading */
  54.     if ((fn = fopen(argv[0], "rb")) == NULL)
  55.         return(1);
  56.  
  57.     /* Return error code if PATCH utility hasn't been run */
  58.     if (antivir.crcs[1] == 0 && antivir.crcs[2] == 0)
  59.         return(2);
  60.  
  61.     /* Calculate and compare the CRC values */
  62.     buff = calloc(antivir.bytes, sizeof(char));
  63.     lowcrc = antivir.lowcrc + sizeof(antivir.code) + 1;
  64.     highcrc = antivir.lowcrc + sizeof(struct avirus);
  65.  
  66.     do {
  67.         rsize = fread (buff, sizeof(char), antivir.bytes, fn);
  68.         buffp = buff;
  69.         crc = 0;
  70.  
  71.         for (j1 = 0; j1 < rsize; j1++) {
  72.             bytecount++;
  73.             if ((bytecount >= lowcrc)  && (bytecount <= highcrc)) {
  74.                 buffp++;
  75.                 crc = crc_update (crc,'\0');
  76.             }
  77.             else
  78.                 crc = crc_update (crc,*(buffp++));
  79.         }
  80.  
  81.         crc = crc_finish(crc);
  82.         if (antivir.crcs[j2++] != crc) {
  83.             change = TRUE;
  84.             break;
  85.         }
  86.  
  87.     } while (rsize == antivir.bytes);
  88.  
  89.     free(buff);
  90.     fclose(fn);
  91.  
  92.     /* Return 0 if file is OK, 3 if it has been altered */
  93.     if (change)
  94.         return(3);
  95.     else
  96.         return(0);
  97. }
  98.  
  99. crc_update(int crcval, char crc_char)
  100. {
  101.     long tmp;
  102.     int  j1;
  103.  
  104.     tmp = ((long)crcval << 8) + crc_char;
  105.     for (j1 = 0; j1 < 8; j1++) {
  106.         tmp = tmp << 1;
  107.         if(tmp & 0x01000000)
  108.             tmp = tmp ^ 0x01800500;
  109.     }
  110.     return((tmp & 0x00ffff00) >> 8);
  111. }
  112.  
  113. crc_finish(int crc)
  114. {
  115.     return(crc_update(crc_update(crc,'\0'), '\0'));
  116. }
  117.  
  118.  
  119.  
  120.